/**
 * 
 */
package com.project.tructc.MBean;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;



import com.project.entity.Account;
import com.project.ryudo.SessionBean.AccountBeanRemote;

/**
 * @author RYUDO
 *
 */
@ManagedBean(name="registrationBean")
@RequestScoped
public class RegistrationBean {
	
	Account acc;
	boolean isRegistered;
	private String confirm_password;
	public RegistrationBean() {
		acc = new Account();
	}

	public void setConfirm_password(String confirm_password) {
		this.confirm_password = confirm_password;
	}

	public String getConfirm_password() {
		return confirm_password;
	}

	public boolean isRegistered() {
		return isRegistered;
	}

	public void setRegistered(boolean isRegistered) {
		this.isRegistered = isRegistered;
	}

	public Account getAcc() {
		return acc;
	}

	public void setAcc(Account acc) {
		this.acc = acc;
	}
	
	public String addNewAccount() throws Exception
	{
		Context ctx = new InitialContext();
		AccountBeanRemote accRe = (AccountBeanRemote) ctx
				.lookup("MySweetHome/AccountBean/remote");
		List emailCheck = accRe.CheckEmail(this.acc.getEmail());
		if(emailCheck.size() > 0)
		{
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,"This email already registered!","This email already registered!");
			FacesContext.getCurrentInstance().addMessage("registration-form:acc_email", message);
			return "failed";
		} 
		if (!this.acc.getPassword().equals(this.confirm_password)) 
		{
			FacesMessage message = new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Password don't match !",
					"Password don't match !");
			FacesContext.getCurrentInstance().addMessage(
					"registration-form:acc_password", message);
			return "failed";
		}
		else
		{
			accRe.addNewAccount(acc);
			return "ok";	
		}
		
			
		
	}
	
	
}
